'use strict';

const Controller = require('egg').Controller;
class ShopController extends Controller {

  async userinfo() {
    const { ctx, app } = this;
    const shopUser = ctx.shopUser;
    let userlist = await app.model.ShopUser.findOne({
      where: {
        id: shopUser.id,
        status: '1'
      },
      include: [
        {
          model: app.model.Shop,
          attributes: ['title', 'logo', 'banner','popularity'],
          include: [
            {
              model: app.model.ShopConfig,
              as:'shop_config',
              attributes: ['customer_total', 'withd_amount','total_amount']
            }
          ]
        }
      ]
    })
    let list = {
      id: userlist.id,
      nickname: userlist.nickname,
      phone:userlist.phone,
      role: userlist.role,
      shop_id: userlist.shop_id,
      shop_title: userlist.shop.title,
      shop_logo: userlist.shop.logo,
      shop_banner: userlist.shop.banner.split(',')[0],
      shop_popularity: userlist.shop.popularity,
      shop_customer: userlist.shop.shop_config.customer_total,
      shop_withd_amount: userlist.shop.shop_config.withd_amount,
      shop_total_amount: userlist.shop.shop_config.total_amount,
    }
    ctx.apiSuccess(list)
  }
  //管理员 核销员列表
  async shopuserlist() {
    const { ctx, app } = this;
    const { usertype } = ctx.query;
    const shopuser = ctx.shopUser;
    ctx.validate({
      usertype: {
        type: 'string',
        required: true,
        range: {
          in: ['shop_supervise','shop_write']
        },
        desc:'用户类型'
      }
    })
    let list = await ctx.page(app.model.ShopUser, { shop_id: shopuser.shop_id, role:usertype})
    ctx.apiSuccess(list)
  }
  //添加管理员 核销员
  async addshopuser() {
    const { ctx, app } = this;
    const shopUser = ctx.shopUser;
    const { usertype, nickname, phone, pwd } = ctx.request.body;
    ctx.validate({
      usertype: {
        type: 'string',
        required: true,
        range: {
          in: ['shop_supervise', 'shop_write']
        },
        desc: '用户类型'
      },
      nickname: {
        type: 'string',
        required: true,
        desc:'昵称'
      },
      phone: {
        type: 'phone',
        required: true,
      
        desc:'手机号'
      },
      pwd: {
        type: 'string',
        required: true,
        range: {
          max:20,
          min:6
        },
        desc:'密码'
      }
    })
    if (shopUser.role == 'shop_write') {
      ctx.throw(400,'你没得权限添加员工账号')
    }
    let checklist = await app.model.ShopUser.findOne({
      where: {
        phone
      }
    })
    if (checklist) {
      ctx.throw(400,'手机号已经存在!')
    }
    let list = await app.model.ShopUser.create({
      shop_id: shopUser.shop_id, nickname, phone, pwd, role: usertype, status:'1'
    })
    ctx.apiSuccess(list)
  }
  //删除 管理员 核销员 
  async delshopuser() {
    const { ctx, app } = this;
    const shopUser = ctx.shopUser;
    const { id, usertype } = ctx.query;
    ctx.validate({
      id: {
        type: 'int',
        required: true,
        desc:'员工id'
      },
      usertype: {
        type: 'string',
        required: true,
        range: {
          in: ['shop_supervise', 'shop_write']
        },
        desc: '用户类型'
      },
    })
    if (shopUser.role == 'shop_write') {
      ctx.throw(400, '你没得权限删除员工账号')
    }
    let list = await app.model.ShopUser.findOne({
      where: {
        role: usertype,
        id
      }
    })
    if (!list) {
      ctx.throw(400,'数据不存在')
    }
    await list.destroy()
    ctx.apiSuccess('删除成功')
  }
  //我的门店
  async myshop() {
    const { ctx, app } = this;
    const shopUser = ctx.shopUser;
    let list = await app.model.Shop.findOne({
      where: {
        id:shopUser.shop_id
      },
      include: [
        {
          model: app.model.Class,
          attributes: ['title']
        }
      ]
    })
    ctx.apiSuccess(list)
  }
  //查看卡券
  async checkcouponslist() {
    const { ctx, app } = this;
    const shopUser = ctx.shopUser;
    const list = await ctx.page(app.model.Coupons, { shop_id: shopUser.shop_id }, {
      attributes: ['id', 'shop_id', 'title', 'pay_price', 'market_price', 'total_number', 'surplus_number', 'statr_time', 'end_time'],
      include: [
        {
          model: app.model.Shop,
          attributes: ['logo']
        }
      ]
    })
    ctx.apiSuccess(list)
  }
  //核销记录
  async writeofflist() {
    const { ctx, app } = this;
    const { coupons_id } = ctx.query;
    const shopUser = ctx.shopUser;
    ctx.validate({
      coupons_id: {
        type: 'int',
        required: false,
        desc:'券id'
      }
    })
    let where = {}, Op = app.Sequelize.Op;
    //卡券全部核销记录
    if (coupons_id == ''||coupons_id==null||coupons_id==undefined) {
      where = { coupons_id: coupons_id, shop_user_id: { [op.gt]:0} }
    } else {//指定核销员 核销记录
      where = {  shop_user_id:shopUser.id }
    }
    let list=await ctx.page(app.model.CouponsCollar,where)
    ctx.apiSuccess(list)
  }
  //扫描数据
  async scancode() {
    const { ctx, app } = this;
    const { id } = ctx.query;
    ctx.validate({
      id: {
        type: 'int',
        required: true,
        desc:'领券表id'
      }
    })
    let list = await app.model.CouponsCollar.findOne({
      where: {
        id
      },
      include: [
        {
          model: app.model.Coupons,
          attributes: ['title','market_price']
        }
        ]
    })
    
    ctx.apiSuccess(list)
  }
}

module.exports = ShopController;
